// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Element/CHEXAParser.h"

#include "gtest/gtest.h"

#include "DataStructure/Input/Element/CHEXAData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"
using namespace SG::DBManager;
using namespace SG::Import;

using namespace SG::DataStructure::FEM;
extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;

class CHEXAParserTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
    }
};

TEST_F (CHEXAParserTest, FirstOrderHex)
{
    std::vector<std::string> Entry = { "CHEXA       1579       1    2170    2255    2256    2171    2187    2272+00020A3",
                                       "+00020A3    2273    2188" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseCHEXA (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::CHEXA8Data data;

    DB_SERVICEFACTORY->get<IElementService> ()->FindById (1579, data);

    ASSERT_TRUE (data.m_id == 1579);
    ASSERT_TRUE (data.m_pId == 1);
    ASSERT_TRUE (data.m_g[0] == 2170);
    ASSERT_TRUE (data.m_g[1] == 2255);
    ASSERT_TRUE (data.m_g[2] == 2256);
    ASSERT_TRUE (data.m_g[3] == 2171);
    ASSERT_TRUE (data.m_g[4] == 2187);
    ASSERT_TRUE (data.m_g[5] == 2272);
    ASSERT_TRUE (data.m_g[6] == 2273);
    ASSERT_TRUE (data.m_g[7] == 2188);
}

TEST_F (CHEXAParserTest, SecondOrderHex)
{
    std::vector<std::string> Entry = { "CHEXA       2594       1    3606    3691    3692    3607    3623    3708+       ",
                                       "+           3709    3624    5343    5669    5348    5339    5341    5671+       ",
                                       "+           5675    5346    5429    5734    5434    5425" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    SG::Import::BDF::BULKDATA::ParseCHEXA (DB_SERVICEFACTORY, dividedEntryInfo);
    SG::DataStructure::FEM::CHEXA20Data data;

    DB_SERVICEFACTORY->get<IElementService> ()->FindById (2594, data);

    ASSERT_TRUE (data.m_id == 2594);
    ASSERT_TRUE (data.m_pId == 1);
    ASSERT_TRUE (data.m_g[0] == 3606);
    ASSERT_TRUE (data.m_g[1] == 3691);
    ASSERT_TRUE (data.m_g[2] == 3692);
    ASSERT_TRUE (data.m_g[3] == 3607);
    ASSERT_TRUE (data.m_g[4] == 3623);
    ASSERT_TRUE (data.m_g[5] == 3708);
    ASSERT_TRUE (data.m_g[6] == 3709);
    ASSERT_TRUE (data.m_g[7] == 3624);
    ASSERT_TRUE (data.m_g[8] == 5343);
    ASSERT_TRUE (data.m_g[9] == 5669);
    ASSERT_TRUE (data.m_g[10] == 5348);
    ASSERT_TRUE (data.m_g[11] == 5339);
    ASSERT_TRUE (data.m_g[12] == 5341);
    ASSERT_TRUE (data.m_g[13] == 5671);
    ASSERT_TRUE (data.m_g[14] == 5675);
    ASSERT_TRUE (data.m_g[15] == 5346);
    ASSERT_TRUE (data.m_g[16] == 5429);
    ASSERT_TRUE (data.m_g[17] == 5734);
    ASSERT_TRUE (data.m_g[18] == 5434);
    ASSERT_TRUE (data.m_g[19] == 5425);
}
